/* Copyright 2008 Bas van den Berg
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "yaffut.h"
#include "myassert.h"

#include "Table.h"
#include "color.h"
#include "StringBuilder.h"

using namespace Chronos;
using namespace std;

struct TableTest {
    StringBuilder output;
};


TEST(TableTest, testEmptyTable) {
    Table table(2);
    table.write(output);
    ASSERT_STR_EQUAL("", output);
}


TEST(TableTest, testAddingCompleteRow) {
    Table table(3);
    table.addCell("first");
    table.addCell("second");
    table.addCell("third");
    table.write(output);
    ASSERT_STR_EQUAL("first  second  third\n", output);
}


TEST(TableTest, testChangingMargin) {
    Table table(3);
    table.setMargin(4);
    table.addCell("first");
    table.addCell("second");
    table.addCell("third");
    table.write(output);
    ASSERT_STR_EQUAL("first    second    third\n", output);
}


TEST(TableTest, testAddingIncompletRow) {
    Table table(3);
    table.addCell("first");
    table.addCell("second");
    table.write(output);
    ASSERT_STR_EQUAL("first  second\n", output);
}


TEST(TableTest, testAddingSeveralCompleteRows) {
    Table table(2);
    table.addCell("first");
    table.addCell("second");
    table.addCell("first column");
    table.addCell("second column");
    table.addCell("third row a");
    table.addCell("third row b");
    table.write(output);
    ASSERT_STR_EQUAL("first         second\n"
                     "first column  second column\n"
                     "third row a   third row b\n", output);
}


TEST(TableTest, testAddingSeveralRowsInComplete) {
    Table table(2);
    table.addCell("first");
    table.addCell("second");
    table.addCell("first column");
    table.addCell("second column");
    table.addCell("third row a");
    table.write(output);
    ASSERT_STR_EQUAL("first         second\n"
                     "first column  second column\n"
                     "third row a\n", output);
}


TEST(TableTest, testLeftLayout) {
    Table table(2, Alignment::Left);
    table.addCell("12345678910");
    table.addCell("abcdefghijklmn");
    table.addCell("first");
    table.addCell("second");
    table.write(output);
    ASSERT_STR_EQUAL("12345678910  abcdefghijklmn\n"
                     "first        second\n", output);
}


TEST(TableTest, testCenterLayout) {
    Table table(2, Alignment::Center);
    table.addCell("12345678910");
    table.addCell("abcdefghijklmn");
    table.addCell("first");
    table.addCell("second");
    table.write(output);
    ASSERT_STR_EQUAL("12345678910  abcdefghijklmn\n"
                     "   first         second\n", output);
}


TEST(TableTest, testRightLayout) {
    Table table(2, Alignment::Right);
    table.addCell("12345678910");
    table.addCell("abcdefghijklmn");
    table.addCell("first");
    table.addCell("second");
    table.write(output);
    ASSERT_STR_EQUAL("12345678910  abcdefghijklmn\n"
                     "      first          second\n", output);
}


TEST(TableTest, testColor) {
    Table table(2);
    table.addCell("first");
    table.addCell("second");
    table.addCell(ANSI_GREEN"first"ANSI_NORMAL);
    table.addCell(ANSI_RED"second"ANSI_NORMAL);
    table.write(output);
    ASSERT_STR_EQUAL("first  second\n"
           ANSI_GREEN"first"ANSI_NORMAL"  "ANSI_RED"second"ANSI_NORMAL"\n", output);
}


TEST(TableTest, testInsertingInts) {
    Table table(3);
    table.addCell(0);
    table.addCell(123);
    table.addCell("END");
    table.addCell(45678);
    table.addCell(9);
    table.addCell("END");
    table.write(output);
    ASSERT_STR_EQUAL("0      123  END\n45678  9    END\n", output);
}

